/**
 * Created by chris on 17-5-13.
 */
var postFee = 0;

$(function () {
    var jsonStr = $('#jsonStr').val();
    var item = eval('(' + jsonStr + ')');
    var data = item.data;
    $('#goodsname').val(data.Title);
    $('#shoptitle').val(data.Shop);
    $('#price').val(data.Price);
    postFee = parseFloat(data.PostFee);
    $('#postfee').val(postFee);
    var total = parseFloat(data.Price) + parseFloat(data.PostFee);
    $('#totalprice').val(total);
    $('#goodspic').attr('src', data.PicUrl);

    var GoodsShowSkuItems = data.GoodsShowSkuItems;
    var Skus = data.Skus;

    var moreinfo = $('#more-info');
    //产生选项
    $(GoodsShowSkuItems).each(function () {
        var tmp = '<div class="form-group"><label class="col-md-2 f-bold control-label">' + $(this)[0].PropName + '</label><div class="col-md-8">';
        if ($(this)[0].PropertyAliasList.length !== 0) {
            var PropertyAliasList = $(this)[0].PropertyAliasList;
            $(PropertyAliasList).each(function () {
                tmp += ' <label class="control-label f-bold"><i class="input_style radio_bg"><input type="radio" name="' + $(this)[0].PropId + '" value="'
                    + $(this)[0].PropValueId + '" title="' + $(this)[0].CustomName + '"></i> '
                    + getimage($(this)[0]) + '</label>';
            });
        }
        tmp += '</div></div>';
        moreinfo.append(tmp);
    });
    var checkeds = [];
    var properties = "";
    //点击事件
    $('input[type=radio]').click(function () {
        checkeds = $('input[type=radio]:checked');
        properties = "";
        for (var i = 0; i < checkeds.length; i++) {
            properties += $(checkeds[i]).attr('name') + ":" + $(checkeds[i]).val();
            if (i < checkeds.length - 1) {
                properties += ";"
            }
        }

        $.each(Skus, function (index, value, array) {
            if (value.Properties === properties) {
                $('#price').val(value.Price);
                var num = $('#nums').val();
                var totalprice = parseFloat(value.Price) * parseFloat(num) + postFee;
                $('#totalprice').val(totalprice);
            }
        });
    });

    $("#pickup").click(function () {
        $(this).addClass('hidden');
        $("#description").addClass('hidden');
    });

    $("#more").click(function () {
        $('#pickup').removeClass('hidden');
        $("#description").removeClass('hidden');
    });


    $('#add_to_car').click(function () {
        var flag = false;
        var skuItem;
        $.each(Skus, function (index, value, array) {
            if (value.Properties === properties) {
                skuItem = value;
                flag = true;
            }
        });
        if (flag) {
            var Title = data.Title;
            var PicUrl = data.PicUrl;
            skuItem.Title = Title + "(" + $('#remark').val() + ")";
            skuItem.PicUrl = PicUrl;
            confirm("确定要添加至购物车吗?", "", function (isConfirm) {
                if (isConfirm) {
                    var prams = $.param({
                        title: Title,
                        purgoods: JSON.stringify(data),
                        skuId: skuItem.SkuId,
                        buycar: JSON.stringify(skuItem),
                        uniprice: $('#price').val(),
                        totalprice: $('#totalprice').val(),
                        number: $('#nums').val()
                    }, true);
                    console.log(prams);
                    $.ajax({
                        url: '/addToCar.action',
                        type: "post",
                        data: prams,
                        dataType: 'json',
                        success: function (data) {
                            var obj = eval('(' + data + ')');
                            if (obj.status === 1) {
                                alert(obj.msg, "", function () {
                                    confirm("请问是否前往购物车结账?", "", function (isConfirm) {
                                        if (isConfirm) {
                                            window.location.href = "/front/mybuycar/buycarlist";
                                        }
                                    }, {type: 'question', confirmButtonText: '确定', cancelButtonText: '取消'});
                                }, {type: 'success', confirmButtonText: '确定'});
                            } else {
                                alert(obj.msg, "", function () {
                                }, {type: 'error', confirmButtonText: '确定'});
                            }
                        }
                    });
                }
            }, {type: 'warning', confirmButtonText: '确定', cancelButtonText: '取消'});
        } else {
            alert("不存在该搭配!", "", function () {
            }, {type: 'warning', confirmButtonText: '确定'});
        }
    })

});
function getimage(item) {
    if (item.IsImage) {
        return '<img src="' + item.SmallImage + '">'
    }
    else return item.CustomName;
}
function increase() {
    var num = $('#nums').val();
    var number = /^\+?[1-9][0-9]*$/;
    if (number.test(num)) {
        if (num <= 0) {
            num = 0;
        }
        var price = $('#price').val();
        num++;
        var totalprice = parseFloat(price) * parseFloat(num) + postFee;
        $('#totalprice').val(totalprice);
        $('#nums').val(num);
    } else {
        $('#nums').focus();
    }
}
function decrease() {
    var num = $('#nums').val();
    var number = /^\+?[1-9][0-9]*$/;
    if (number.test(num)) {
        if (num > 1) {
            var price = $('#price').val();
            num--;
            var totalprice = parseFloat(price) * parseFloat(num) + postFee;
            $('#totalprice').val(totalprice);
            $('#nums').val(num);
        }
    } else {
        $('#nums').focus();
    }
}